package logger

import (
	"fmt"
	"github.com/lestrrat-go/file-rotatelogs"
	"github.com/sirupsen/logrus"
	"io"
	"os"
	"path"
	"runtime"
	"time"
)

import _ "github.com/jinzhu/gorm/dialects/mysql"

var myLogger *logrus.Logger

func Logger() *logrus.Logger {
	return myLogger
}

func init() {
	rl, _ := rotatelogs.New("/logs/access_log.%Y%m%d%H%M", rotatelogs.WithRotationTime(24*time.Hour))

	log := logrus.New()
	log.SetReportCaller(true)
	log.Formatter = &logrus.JSONFormatter{
		CallerPrettyfier: func(f *runtime.Frame) (string, string) {
			filename := path.Base(f.File)
			return fmt.Sprintf("%s()", f.Function), fmt.Sprintf("%s:%d", filename, f.Line)
		},
	}

	mw := io.MultiWriter(os.Stdout, rl)
	//log.SetFormatter(&logrus.JSONFormatter{})
	log.SetLevel(logrus.DebugLevel)
	log.SetOutput(mw)
	myLogger = log
}
